package com.demo;

import com.demo.infrastructure.gateway.ICSDNService;
import com.demo.type.properties.CSDNApiProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

@Slf4j
@SpringBootApplication
public class McpServerApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(McpServerApplication.class, args);
    }

    /**
     * 创建ICSDNService的Bean，用于调用CSDN API
     * @return ICSDNService实例
     */
    @Bean
    public ICSDNService csdnService() {
        // 创建Retrofit实例并配置基础URL和转换器
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://bizapi.csdn.net/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        // 创建并返回ICSDNService实例
        return retrofit.create(ICSDNService.class);
    }

    @Resource
    private CSDNApiProperties properties;

    @Override
    public void run(String... args) throws Exception {
        log.info("check csdn cookie ...");
        if (properties.getCookie() == null) {
            log.warn("csdn cookie key is null, please set it in application.yml");
        } else {
            log.info("csdn cookie  key is {}", properties.getCookie());
        }
    }
}
